<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ReservationController extends Controller
{
	//会议室预约
	public function getIndex(Request $request)
	{
		if($request['name']){
			$ind = DB::table('meetingroomreservation')
			->join('meetingroom','meetingroomreservation.MeetingRoomID','=','meetingroom.id')
			->join('department','meetingroomreservation.DepartmentID','=','department.id')
			->where('MeetingRoomName','like','%'.$request['name'].'%')
            ->paginate(10);
		}else{
			$ind = DB::table('meetingroomreservation')
			->join('meetingroom','meetingroomreservation.MeetingRoomID','=','meetingroom.id')
			->join('department','meetingroomreservation.DepartmentID','=','department.id')
            ->paginate(3);
		}

		// dd($ind);
		 $info = $request -> all();
		return view('Lzk.Reservation.Reservation',['ind'=>$ind,'info'=>$info]);
	}

	//解析添加
	public function getAdd()
	{
		$add = DB::table('meetingroom')->get();
		$depart = DB::table('department')->get();
		// dd($depart);
		
		return view('Lzk.Reservation.AddReservation',['add'=>$add,'depart'=>$depart]);
	}

	//执行添加
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$ins = $request->only('MeetingRoomID','DepartmentID','ReservationTime','ReservationTime1');
		$ins['Date'] = time();
		$xian = $ins['Date'];
		$statime = date("Y-m-d H:i:s", $xian);
		// dd($statime);
		$mid = $ins['MeetingRoomID'];
		$one = $ins['ReservationTime'];
		$two = $ins['ReservationTime1'];
		// $idd = $ins['MeetingRoomID'];
		// dd($one);
		


		if($one > $two){
			echo '<script>alert("输入正确的结束时间");window.location.href="/Reservation/add";</script>';
			die;
		}
		// if($one < $statime){
		// 	echo '<script>alert("输入正确的预约时间");window.location.href="/Reservation/add";</script>';
		// 	die;
		// }
		
		//获取当前传进来的小时
		$b = substr($ins['ReservationTime'],11,2);
		if($b < 12){
			$ins['ReservationDate'] = 1;
		}else{
			$ins['ReservationDate'] = 2;
		}

		$res1 = DB::table('meetingroomreservation')->where('MeetingRoomID',$mid)->first();
		// dd($res1);
		if($res1){
			//获取数据库中开始 结束 时间
			$go = $res1['ReservationTime'];
			$end = $res1['ReservationTime1'];
			if($one >= $go && $one <= $end){
				echo '<script>alert("此阶段已预约,请重新选择时间");window.location.href="/Reservation/add";</script>';
				die;
			}else if($two >= $go && $two <= $end){
				echo '<script>alert("此阶段已预约,请重新选择时间");window.location.href="/Reservation/add";</script>';
				die;	

			}
		}
		
		$res = DB::table('meetingroomreservation')->insert($ins);
			if($res){
				return redirect('/Reservation');
			}else{
				return back();
			}

	}

	//会议室预约删除
	public function getDel($id)
	{
		$del = DB::select("delete from meetingroomreservation where ids = '{$id}'");
		if($del){
			return redirect('/Reservation');
		}else{
			return back();
		}

	}

	//会议室修改
	public function getEdit($id)
	{
		
		// $edit = DB::table('meetingroomreservation')->where('ids',$id)->first();
		// dd($edit);
		$edits = DB::table('meetingroomreservation')
			->join('meetingroom','meetingroomreservation.MeetingRoomID','=','meetingroom.id')
			->join('department','meetingroomreservation.DepartmentID','=','department.id')
			->where('ids',$id)
			->get();
		// dd($edits);
		return view('Lzk.Reservation.EditReservation',['edits'=>$edits]);					 
	}

	//执行修改
	public function postUpdate(Request $request)
	{
		// dd($request->all());
		$uid = $request->only('ids');
		// dd($uid);
		$upda = $request->only('ReservationTime','ReservationTime1');
		// dd($upda);
		$upda['Date'] = time();

		$timeone = $upda['ReservationTime'];
		$timetwo = $upda['ReservationTime1'];
		if($timeone > $timetwo){
			echo '<script>alert("输入正确的结束时间");window.location.href="/Reservation";</script>';
			die;
		}

		$ub = substr($upda['ReservationTime'],11,2);
		if($ub < 12){
			$upda['ReservationDate'] = 1;
		}else{
			$upda['ReservationDate'] = 2;
		}

		$updas = DB::table('meetingroomreservation')->where('ids',$uid)->update($upda);
        if($updas){
        	return redirect('/Reservation')->with('修改成功');
        }else{
        	return back('/Reservation/edit');
        }
	}



}
